package com.itheima.zipstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipOutputStreamDemo2 {
    public static void main(String[] args) throws IOException {
        //压缩流
        //需求:
        //把D：\\a.txt打包成一个压缩包
        File src = new File("D:\\a.txt");
        File dest = new File("D:\\");

        toZip(src,dest);
    }

    private static void toZip(File src, File dest) throws IOException {
        //创建压缩流关联压缩包
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest,"a.zip")));
        //创建zipEntry对象表示压缩包里面的文件或文件夹
        ZipEntry entry = new ZipEntry("a.txt");
        //将zipentry对象添加到压缩包中去
        zos.putNextEntry(entry);
        //往压缩包的文件写入数据
        FileInputStream fis = new FileInputStream(src);
        int b;
        while ((b=fis.read())!=-1){
            zos.write(b);
        }
        zos.closeEntry();
        fis.close();
        zos.close();
    }
}
